const MyPromise = require("../template.js")

/** 九、Promise.resolve 静态方法测试 */

// 测试 1: resolve 普通值
function testResolveValue() {
  MyPromise.resolve(42).then((value) =>
    console.log("9.1 Resolve value:", value === 42)
  )
}

// 测试 2: resolve Promise
function testResolvePromise() {
  const innerPromise = new MyPromise((resolve) => resolve("Inner"))
  MyPromise.resolve(innerPromise).then((value) =>
    console.log("9.2 Resolve Promise:", value === "Inner")
  )
}

// 测试 3: resolve thenable 对象
function testResolveThenable() {
  const thenable = {
    then: (resolve) => {
      setTimeout(() => resolve("Thenable"), 50)
    }
  }
  MyPromise.resolve(thenable).then((value) =>
    console.log("9.3 Resolve thenable:", value === "Thenable")
  )
}

// 测试 4: resolve 字符串
function testResolveString() {
  MyPromise.resolve("Hello").then((value) =>
    console.log("9.4 Resolve string:", value === "Hello")
  )
}

// 测试 5: resolve 对象
function testResolveObject() {
  const obj = { name: "Test" }
  MyPromise.resolve(obj).then((value) =>
    console.log("9.5 Resolve object:", value.name === "Test")
  )
}

// 测试 6: resolve 数组
function testResolveArray() {
  const arr = [1, 2, 3]
  MyPromise.resolve(arr).then((value) =>
    console.log("9.6 Resolve array:", value.length === 3 && value[0] === 1)
  )
}

// 测试 7: resolve undefined
function testResolveUndefined() {
  MyPromise.resolve(undefined).then((value) =>
    console.log("9.7 Resolve undefined:", value === undefined)
  )
}

// 测试 8: resolve null
function testResolveNull() {
  MyPromise.resolve(null).then((value) =>
    console.log("9.8 Resolve null:", value === null)
  )
}

// 测试 9: resolve 链式调用
function testResolveChain() {
  MyPromise.resolve(1)
    .then((x) => x + 1)
    .then((x) => x * 2)
    .then((x) => console.log("9.9 Resolve chain:", x === 4))
}

// 测试 10: resolve rejected Promise
function testResolveRejectedPromise() {
  const rejectedPromise = MyPromise.reject("Error")
  MyPromise.resolve(rejectedPromise).then(
    (value) => console.log("9.10 Resolve rejected Promise:", false),
    (reason) => console.log("9.10 Resolve rejected Promise:", reason === "Error")
  )
}

testResolveValue()
testResolvePromise()
testResolveThenable()
testResolveString()
testResolveObject()
testResolveArray()
testResolveUndefined()
testResolveNull()
testResolveChain()
testResolveRejectedPromise()
